<?php
class ControllerCatalogCategory extends AdminController {

	public function index() {
		M('catalog/category');
		$this->getList();
	}

	public function insert() {

		M('catalog/category');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_category->addCategory($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/category', "category_id=" . $this->request->post['parent_id']));
		}

		$this->getForm();
	}

	public function update() {

		M('catalog/category');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_category->editCategory($this->request->get['category_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);

			$this->redirect(UA('catalog/category', "category_id=" . $this->request->post['parent_id']));
		}

		$this->getForm();
	}

	public function delete() {

		M('catalog/category');
		if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $category_id) {
				$this->model_catalog_category->deleteCategory($category_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/category'));
		}
		$this->getList();
	}

	public function update_cache() {
		M('catalog/category');
		//call twice
		$this->model_catalog_category->cache();
		$this->model_catalog_category->cache();
		$this->session->set_flashdata('success', $this->_['text_cache_success']);
		$this->redirect(UA('catalog/category'));
	}

	protected function getList() {
		global $CATEGORIES;

		$this->document->setTitle($this->_['heading_title']);
		$category_id = G('category_id', 0);

		$this->data['breadcrumbs'] = array();

		if ($category_id && isset($CATEGORIES[$category_id])) {
			foreach(explode(',', $CATEGORIES[$category_id]['path']) as $id) {
				$this->data['breadcrumbs'][] = array(
					'text'      => $CATEGORIES[$id]['name'],
					'href'      => UA('catalog/category', 'category_id=' . $id),
					'separator' => ' :: '
				);
			}
		}

		$this->data['categories'] = $this->model_catalog_category->getCategories($category_id, false);

		foreach ($this->data['categories'] as $i => $category) {
			$this->data['categories'][$i]['total'] = isset($CATEGORIES[$category['category_id']]) ? $CATEGORIES[$category['category_id']]['total']: 0; 
		}

		$this->display('catalog/category_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		$this->document->addScript(ROOT . 'static/javascript/jquery/jquery.validate.js');

		$this->data['token'] = $this->session->data['token'];

		$category_id = G('category_id', 0);

		$categories = $this->model_catalog_category->getCategories(0);
		
		if ($category_id) {
			$this->data['action'] = UA('catalog/category/update', 'category_id=' . $category_id);
			
			if (!$this->request->isPost()) {
				$category_info = $this->model_catalog_category->getCategory($category_id);
				foreach ($categories as $key => $category) {
					if ($category['category_id'] == $category_info['category_id']) {
						unset($categories[$key]);
					}
				}
				$this->vo->data = $category_info;
				$this->vo->data['category_description'] = $this->model_catalog_category->getCategoryDescriptions($category_id);
			}
		} else {
			$this->data['action'] = UA('catalog/category/insert');
		}

		$this->data['categories'] = $categories;
		
		
		if (!empty($category_info)) {
			$this->data['category_store'] = $this->model_catalog_category->getCategoryStores($category_id);
			$this->data['category_layout'] = $this->model_catalog_category->getCategoryLayouts($category_id);
		}
		else {
			$this->data['category_store'] = P('category_store', array(0));
			$this->data['category_layout'] = P('category_layout', array());
		}

		$this->data['category_attribute'] = array();		
		$attribute_ids = $this->vo->get('attribute_ids');
		if ($attribute_ids) {
				M('catalog/attribute');
				foreach(explode(',', $attribute_ids) as $attribute_id) {
					$attribute_description = $this->model_catalog_attribute->getAttributeDescriptions($attribute_id);
					$this->data['category_attribute'][] = array(
						'attribute_id' => $attribute_id,
						'name' => $attribute_description[C('config_language_id')]['name']
					);
				}
		}

		//languages
		$this->data['languages'] = C('cache_language_2');
		
		//stores
		M('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		//layouts
		M('design/layout');
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
		
		$this->display('catalog/category_form.tpl');
	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
		foreach ($this->request->post['category_description'] as $language_id => $value) {
			$this->form_validation->set_rules("category_description[$language_id][name]", '', 'required|range_length[3,255]', $this->_['error_name']);
		}
		if ($this->form_validation->run()) {
			return true;
		}
		else {
			$this->setMessage('error_warning', $this->_['error_warning']);
			return false;
		}
	}

	public function subCategories() {
		$output = '<option value="0">' . $this->_['text_none'] . '</option>';

		M('catalog/category');
		$categories = $this->model_catalog_category->getCategories($this->request->get['parent_id'], false);
		$output .= form_select_option($categories, null, null, 'category_id', 'name');
		$this->response->setOutput($output);
	}
	
	public function ajax_gen_model() {
		M('catalog/category');
		$data['model'] = $this->model_catalog_category->genModelNum(G('category_id'));
		$this->response->setOutput(json_encode($data));
	}
	
	public function smartedit() {
		if ($this->hasMP()) {
			$category_id = (int) G('id');
			$field = G('field');
			$value = G('val');
			$rs = $this->db->update('category', array($field => $value), "category_id=$category_id");
			
		}
		else $rs = 0;
		$this->response->setOutput(json_encode(array('status' => $rs)));
	}
}
?>